home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 January: Mac OS SDK / Dev.CD Jan 98 SDK1.toast / Development Kits (Disc 1) / AIAT 1.0.1 / Examples / Sources / DemoAccessor.cpp next >
Encoding:
Text File  |  1997-09-11  |  14.5 KB  |  534 lines  |  [TEXT/CWIE]

  1. /// DemoAccessor.cp
  2. //    Copyright:    © 1994 - 1997 by Apple Computer, Inc., all rights reserved.
  3.  
  4.  
  5. #include "DemoAccessor.h"
  6.  
  7. #ifdef DEBUG_NEW
  8. #include <DebugNew.h>
  9. #endif
  10.  
  11. #include <stdio.h>
  12. #include <string.h>
  13.  
  14. #if __profile__
  15. #include <profiler.h>
  16. #endif
  17.  
  18. void PrintDocName(IADoc* doc);
  19. void PrintDocName(IADoc* doc) {
  20.     uint32 docNameLength;
  21.     char* docName = (char*)doc->GetName(&docNameLength);
  22. //    IAAssert(strlen(docName) == docNameLength);
  23.     IAAssertion(strlen(docName) == docNameLength, "mismatch document names", IAAssertionFailure);
  24.     printf("%s", docName);
  25.     IAFreeArray(docName);
  26. }
  27.  
  28. void DemoAccessor::Initialize() {
  29.     time_t initStartTime = time(NULL);
  30.     DEMOACCESSORCLASS::Initialize();
  31.     time_t initTime = time(NULL) - initStartTime;
  32.     struct tm* timeStruct = localtime(&initTime);
  33.     char buffer[80];
  34.     strftime(buffer, 80, "accessor initialization: %H hours, %M minutes and %S seconds.\n", timeStruct);
  35.     printf(buffer);
  36. }
  37.  
  38. bool    DemoAccessor::IsHit(IAIndex* index, const IADoc* doc) {
  39.     #pragma unused(index)
  40.     bool result = true;
  41.     if (firstChar) {
  42.       uint32 docNameLength;
  43.       char* docName = (char*)doc->GetName(&docNameLength);
  44.       if (!docNameLength) {
  45.         result = false;
  46.       } else if (docName[0] != firstChar) {
  47.         result = false;
  48.       }
  49.       IAFreeArray(docName);
  50.     }
  51.     return result;
  52. }
  53.  
  54. //// Calling the IAAccessor
  55.  
  56. bool DemoRankedProgress(const RankedProgress* progress, void* data);
  57. bool DemoRankedProgress(const RankedProgress* progress, void* data) {
  58.     #pragma unused (data)
  59.     printf("searching: %4.1f ", progress->GetPercent());
  60.     if (progress->GetTerm()) {
  61.       printf("%s", (char*)progress->GetTerm()->GetData());
  62.     }
  63.     if (progress->GetDocument()) {
  64.       PrintDocName(progress->GetDocument());
  65.     }
  66.     printf("\n");
  67.     return false;
  68. }
  69.  
  70. const MaxResultCount = 15;
  71. const uint32 kMaxDocuments = 10;
  72.  
  73. void DemoRankedSearchInternal (DemoAccessor* accessor, char* query, IADoc** feedback, uint32 nrq);
  74. void DemoRankedSearchInternal (DemoAccessor* accessor, char* query, IADoc** feedback, uint32 nrq) {
  75.     RankedQueryDoc rqd1[kMaxDocuments];
  76.     if (nrq > kMaxDocuments) nrq = kMaxDocuments;
  77.     printf("Query: %s\n", query);                            // display query
  78.     for (int i = 0; i < nrq; i ++) {
  79.     printf("Feedback: ");
  80.       PrintDocName(feedback[i]);
  81.       printf("\n");
  82.     }
  83.  
  84.     time_t start = time(NULL);
  85.     
  86.  
  87.     RankedHit* results[MaxResultCount];                    // allocate array for results
  88.     // RankedQueryDoc rqd(feedback, TermIndex::IANarrow(accessor->indices[0]));
  89.     
  90.     for (int i = 0; i <nrq; i++) {
  91.         rqd1[i].doc = feedback[i];
  92.         rqd1[i].index =  TermIndex::IANarrow(accessor->GetIndices()[0]);
  93.     }
  94.     // execute query
  95.     uint32 resultCount = accessor->RankedSearch((byte*)query, strlen(query),    // query string
  96.                                                 rqd1, nrq,            // feedback doc
  97.                                                 results, MaxResultCount, 4,        // result array
  98.                                                 &DemoRankedProgress, 30, NULL);    // progress args
  99.  
  100.     time_t duration = time(NULL) - start;
  101.     struct tm* timeStruct = localtime(&duration);
  102.     char buffer[80];
  103.     strftime(buffer, 80, "search time: %H hours, %M minutes and %S seconds.\n", timeStruct);
  104.     printf(buffer);
  105.  
  106.     printf("%lu hits\n", resultCount);                    // display results
  107.     for (uint32 i = 0; i < resultCount; i++) {
  108.       RankedHit* hit = results[i];
  109.       printf("%5.2f : ", hit->GetScore());
  110.       PrintDocName(hit->GetDocument());
  111.       printf(" [");
  112.       for (uint32 j = 0; j < hit->GetMatchingTermsLen(); j++)
  113.         printf(" %s", hit->GetMatchingTerms()[j]->GetData());
  114.       printf("]\n");
  115.       delete hit;
  116.     }
  117. }
  118.  
  119. void DemoRankedSearch (char* query, IADoc* feedback = NULL, char firstChar = NULL);
  120. void DemoRankedSearch (char* query, IADoc* feedback, char firstChar) {
  121.     // make a storage
  122.     IAStorage* storage = DEMOSTORAGE(DEMOINDEXFILENAME);
  123.     IADeleteOnUnwind delStorage(storage);
  124.     storage->Open(false);
  125.     // make an index in this storage
  126.     DemoIndex index(storage);
  127.     index.Open();
  128. /*
  129.     // make a second storage
  130.     IAStorage* storage2 = DEMOSTORAGE("\p2.index");
  131.     IADeleteOnUnwind delStorage2(storage2);
  132.     storage2->Open(false);
  133.     // make a second index in this storage
  134.     DemoIndex index2(storage2);
  135.     index2.Open();
  136. */
  137.     const uint32 nIndices = 1;
  138.     DEMOACCESSORINDEXCLASS* indices[nIndices];        // make indices
  139.     indices[0] = &index;
  140.     //indices[1] = &index2;
  141.  
  142.     DemoAccessor accessor(indices, nIndices);        // make appropriate accessor
  143.     accessor.firstChar = firstChar;
  144.     accessor.Initialize();
  145.  
  146.     DemoRankedSearchInternal(&accessor, query, &feedback, 0);
  147. }
  148.  
  149. void DemoBooleanRankedSearchInternal (DemoAccessor* accessor, char* query);
  150. void DemoBooleanRankedSearchInternal (DemoAccessor* accessor, char* query) {
  151.     RankedQueryDoc rqd1[kMaxDocuments];
  152.  
  153.     printf("Query: %s\n", query);                            // display query
  154.  
  155.     time_t start = time(NULL);
  156.     
  157.  
  158.     RankedHit* results[MaxResultCount];                    // allocate array for results
  159.     uint32 resultCount = 0;
  160.     // RankedQueryDoc rqd(feedback, TermIndex::IANarrow(accessor->indices[0]));
  161. #ifdef DEMOINDEXISINVERTED
  162.     
  163.     // execute query
  164. ((InvertedAccessor*)accessor)->SetBooleanAndOperator('+');
  165. ((InvertedAccessor*)accessor)->SetBooleanOrOperator('|');
  166. ((InvertedAccessor*)accessor)->SetBooleanNotOperator('-');
  167. ((InvertedAccessor*)accessor)->SetBooleanLeftFence('[');
  168. ((InvertedAccessor*)accessor)->SetBooleanRightFence(']');
  169.  
  170.     resultCount = ((InvertedAccessor*)accessor)->RankedSearchBoolean((byte*)query, strlen(query),    // query string
  171.                                                 results, MaxResultCount,         // result array
  172.                                                 &DemoRankedProgress, 30, NULL);    // progress args
  173. #endif
  174.  
  175.     time_t duration = time(NULL) - start;
  176.     struct tm* timeStruct = localtime(&duration);
  177.     char buffer[80];
  178.     strftime(buffer, 80, "search time: %H hours, %M minutes and %S seconds.\n", timeStruct);
  179.     printf(buffer);
  180.  
  181.     printf("%lu hits\n", resultCount);                    // display results
  182.     for (uint32 i = 0; i < resultCount; i++) {
  183.       RankedHit* hit = results[i];
  184.       printf("%5.2f : ", hit->GetScore());
  185.       PrintDocName(hit->GetDocument());
  186.        printf("\n");
  187.       delete hit;
  188.     }
  189. }
  190.  
  191. void DemoBooleanRankedSearch (char* query,  char firstChar = NULL);
  192. void DemoBooleanRankedSearch (char* query,  char firstChar) {
  193.     // make a storage
  194.     IAStorage* storage = DEMOSTORAGE(DEMOINDEXFILENAME);
  195.     IADeleteOnUnwind delStorage(storage);
  196.     storage->Open(false);
  197.     // make an index in this storage
  198.     DemoIndex index(storage);
  199.     index.Open();
  200. /*
  201.     // make a second storage
  202.     IAStorage* storage2 = DEMOSTORAGE("\p2.index");
  203.     IADeleteOnUnwind delStorage2(storage2);
  204.     storage2->Open(false);
  205.     // make a second index in this storage
  206.     DemoIndex index2(storage2);
  207.     index2.Open();
  208. */
  209.     const uint32 nIndices = 1;
  210.     DEMOACCESSORINDEXCLASS* indices[nIndices];        // make indices
  211.     indices[0] = &index;
  212.     //indices[1] = &index2;
  213.  
  214.     DemoAccessor accessor(indices, nIndices);        // make appropriate accessor
  215.     accessor.firstChar = firstChar;
  216.     accessor.Initialize();
  217.  
  218.     DemoBooleanRankedSearchInternal(&accessor, query);
  219. }
  220.  
  221. void DemoFeedback (char firstChar = NULL);
  222. void DemoFeedback (char firstChar) {
  223.     // make a storage
  224.     IAStorage* storage = DEMOSTORAGE(DEMOINDEXFILENAME);
  225.     IADeleteOnUnwind delStorage(storage);
  226.     storage->Open(false);
  227.     // make an index in this storage
  228.     DemoIndex index(storage);
  229.     index.Open();
  230.  
  231.     const uint32 nIndices = 1;
  232.     DEMOACCESSORINDEXCLASS* indices[nIndices];            // make indices
  233.     indices[0] = &index;
  234.  
  235.     DemoAccessor accessor(indices, nIndices);        // make appropriate accessor
  236.     accessor.firstChar = firstChar;
  237.     accessor.Initialize();
  238.  
  239.     DocID maxID = index.GetMaxDocID();
  240.     if (maxID > 1) {
  241.       IADoc* doc = index.GetIDDoc(maxID / 2);
  242.       IADeleteOnUnwind delDoc(doc);
  243.       DemoRankedSearchInternal(&accessor, "", &doc, 1);
  244.     }
  245. }
  246.  
  247.  
  248. void DemoFeedbacks(char firstChar = NULL);
  249. void DemoFeedbacks(char firstChar) {
  250.     // make a storage
  251.     IAStorage* storage = DEMOSTORAGE(DEMOINDEXFILENAME);
  252.     IADeleteOnUnwind delStorage(storage);
  253.     storage->Open(false);
  254.     // make an index in this storage
  255.     DemoIndex index(storage);
  256.     index.Open();
  257.  
  258.     const uint32 nIndices = 1;
  259.     const uint32 ndocs = 3;
  260.     const uint32 loopControl = 2;
  261.     HFSDoc* fbdocs[kMaxDocuments];
  262.     DEMOACCESSORINDEXCLASS* indices[nIndices];            // make indices
  263.     indices[0] = &index;
  264.  
  265.     DemoAccessor accessor(indices, nIndices);        // make appropriate accessor
  266.     accessor.firstChar = firstChar;
  267.     accessor.Initialize();
  268.  
  269.     IAOrderedStorableIterator* docs = index.GetDocInfoIterator();
  270.     IADeleteOnUnwind delDocs(docs);
  271.     
  272. uint32 j = 0;
  273.     for (DocInfo* di = (DocInfo*)docs->Next(); di; di = (DocInfo*)docs->Next()) {
  274.       IADeleteOnUnwind delDi(di);
  275.         for (int i = 0; i < ndocs; i++) {
  276.             fbdocs[i] = (HFSDoc*) di->GetDocument();
  277.             di = (DocInfo*)docs->Next();
  278.         }
  279.       DemoRankedSearchInternal(&accessor, "", (IADoc**)fbdocs, ndocs);
  280.         if (++j > loopControl) break;
  281.     }
  282. }
  283.  
  284. void DemoGetDocTopic();
  285. void DemoGetDocTopic() {
  286.     // make a storage
  287.     IAStorage* storage = DEMOSTORAGE(DEMOINDEXFILENAME);
  288.     IADeleteOnUnwind delStorage(storage);
  289.     storage->Open(false);
  290.     // make an index in this storage
  291.     DemoIndex index(storage);
  292.     index.Open();
  293.  
  294.     const uint32 nIndices = 1;
  295.     DEMOACCESSORINDEXCLASS* indices[nIndices];                // make indices
  296.     indices[0] = &index;
  297.  
  298.     DemoAccessor accessor(indices, nIndices);                // make appropriate accessor
  299.     accessor.Initialize();
  300.  
  301.     IATerm* results[MaxResultCount];                        // allocate array for results
  302.  
  303.     DocID maxID = index.GetMaxDocID();
  304.     if (maxID > 1) {
  305.       IADoc* doc = index.GetIDDoc(maxID / 2);
  306.       IADeleteOnUnwind delDoc(doc);
  307.       printf("Doc: ");
  308.       PrintDocName(doc);
  309.       printf("\n");
  310.  
  311.       time_t start = ::time(NULL);
  312.  
  313.       RankedQueryDoc rqd(doc, &index);
  314.       uint32 resultCount = accessor.GetDocTopic(    &rqd, results, MaxResultCount,
  315.                                                     &DemoRankedProgress, 30, NULL);
  316.       time_t duration = ::time(NULL) - start;
  317.       struct tm* timeStruct = localtime(&duration);
  318.       char buffer[80];
  319.       strftime(buffer, 80, "time: %H hours, %M minutes and %S seconds.\n", timeStruct);
  320.       printf(buffer);
  321.  
  322.       printf("terms:");
  323.       for (uint32 i = 0; i < resultCount; i++) {
  324.         IATerm* term = results[i];
  325.         printf(" %s", term->GetData());
  326.         delete term;
  327.       }
  328.       printf("\n");
  329.     }
  330. }
  331.  
  332. void DemoStoreInits (bool force = false);
  333. void DemoStoreInits (bool force) {
  334.     // make a storage
  335.     IAStorage* storage = DEMOSTORAGE(DEMOINDEXFILENAME);
  336.     IADeleteOnUnwind delStorage(storage);
  337.     storage->Open(true);                            // note: opened writably
  338.     // make an index in this storage
  339.     DemoIndex index(storage);
  340.     index.Open();
  341.  
  342.     const uint32 nIndices = 1;
  343.     DEMOACCESSORINDEXCLASS* indices[nIndices];        // make indices
  344.     indices[0] = &index;
  345.  
  346.     DemoAccessor accessor(indices, nIndices);
  347.  
  348.     if (force || !accessor.IsInitializationValid()) {
  349.       printf("Computing accessor initializations.\n");
  350.       time_t initStartTime = time(NULL);
  351.  
  352.       accessor.StoreInitialization();                // store initialization
  353.       storage->Commit();                            // save changes
  354.  
  355.       time_t initTime = time(NULL) - initStartTime;
  356.       struct tm* timeStruct = localtime(&initTime);
  357.       char buffer[80];
  358.       strftime(buffer, 80, "store inits: %H hours, %M minutes and %S seconds.\n", timeStruct);
  359.       printf(buffer);
  360.     } else {
  361.       printf("Initialization is already up to date.\n");
  362.     }
  363.  
  364. }
  365.  
  366. void DemoDelete ();
  367. void DemoDelete () {
  368.     // make a storage
  369.     IAStorage* storage = DEMOSTORAGE(DEMOINDEXFILENAME);
  370.     IADeleteOnUnwind delStorage(storage);
  371.     storage->Open(true);
  372.     // make an index in this storage
  373.     DemoIndex index(storage);
  374.     index.Open();
  375.  
  376.     DocID maxID = index.GetMaxDocID();
  377.     if (maxID > 1) {
  378.       IADoc* doc = index.GetIDDoc(maxID / 2);
  379.       IADeleteOnUnwind delDoc(doc);
  380.       index.DeleteDoc(doc);
  381.       index.Flush();
  382.       storage->Commit();            // commit the changes
  383.     }
  384. }
  385.  
  386. void DemoDeleteAll ();
  387. void DemoDeleteAll () {
  388.     // make a storage
  389.     IAStorage* storage = DEMOSTORAGE(DEMOINDEXFILENAME);
  390.     IADeleteOnUnwind delStorage(storage);
  391.     storage->Open(true);
  392.     // make an index in this storage
  393.     DemoIndex index(storage);
  394.     index.Open();
  395.  
  396.     for (DocID id = 1; id < index.GetMaxDocID(); id++) {
  397.       IADoc* doc = index.GetIDDoc(id);
  398.       IADeleteOnUnwind delDoc(doc);
  399.       index.DeleteDoc(doc);
  400.     }
  401.     index.Flush();
  402.     storage->Commit();                // commit the changes
  403. }
  404.  
  405.  
  406. void DemoCompact ();
  407. void DemoCompact () {
  408.     // make a storage
  409.     IAStorage* storage = DEMOSTORAGE(DEMOINDEXFILENAME);
  410.     IADeleteOnUnwind delStorage(storage);
  411.     storage->Open(true);
  412.     // make an index in this storage
  413.     DemoIndex index(storage);
  414.     index.Open();
  415.  
  416.     printf("compacting...\n");
  417.     index.Compact();
  418. }
  419.  
  420.  
  421. void DemoRelatedTerms(char* query);
  422. void DemoRelatedTerms(char* query) {
  423.     printf (">>>>>>> The Related Terms %s\n", query);
  424.     // make a storage
  425.     IAStorage* storage = DEMOSTORAGE(DEMOINDEXFILENAME);
  426.     IADeleteOnUnwind delStorage(storage);
  427.     storage->Open(false);
  428.     // make an index in this storage
  429.     DemoIndex index(storage);
  430.     index.Open();
  431.  
  432.     const uint32 nIndices = 1;
  433.     DEMOACCESSORINDEXCLASS* indices[nIndices];                // make indices
  434.     indices[0] = &index;
  435.  
  436.     DemoAccessor accessor(indices, nIndices);                // make appropriate accessor
  437.     accessor.firstChar = '\0';
  438.     accessor.Initialize();
  439.  
  440.     IATerm* termResults[MaxResultCount];                        // allocate array for results
  441.     
  442.     uint32 resultCount = accessor.GetTermsRelated((byte*)query, strlen(query),    // query string
  443.                                                 termResults, MaxResultCount,        // result array
  444.                                                 &DemoRankedProgress, 30, NULL);    // progress args
  445.  
  446.       printf("related terms:");
  447.       for (uint32 i = 0; i < resultCount; i++) {
  448.         IATerm* term = termResults[i];
  449.         printf(" %s", term->GetData());
  450.         delete term;
  451.       }
  452.       printf("\n");
  453.       printf ("<<<<<<< \n");
  454.  
  455. }
  456.  
  457. void DemoOpen();
  458. void DemoOpen () {
  459.     // make a storage
  460.     StringPtr name = "\ptest.index";
  461.     short vRefNum = 0;
  462.     long dirID = 0;
  463.  
  464.     IAStorage * aStorage = MakeHFSStorage(vRefNum, dirID, name);
  465.     IADeleteOnUnwind delInxStorage(aStorage);
  466.     aStorage->Open(true);                            
  467.  
  468.     HFSCorpus* anHFSCorpus = new HFSCorpus(HFSCorpusType);
  469.     InVecIndex anInVecIndex(aStorage, anHFSCorpus, new SimpleAnalysis());
  470.  
  471.     anInVecIndex.Open();
  472. }
  473.  
  474. /// the main procedure
  475.  
  476. void main() {
  477.  
  478. #if __profile__
  479. #ifdef powerc
  480.     ProfilerInit(collectDetailed, PPCTimeBase, 1500, 50);
  481. #else
  482.     ProfilerInit(collectSummary, microsecondsTimeBase, 1500, 50);
  483. #endif    
  484. #endif
  485.  
  486.     IATry {
  487. //        DemoOpen();
  488.         
  489.         DemoStoreInits();   // for profile
  490.  
  491.  
  492.     DemoRankedSearch("water product", NULL);
  493. //        DemoRankedSearch("Furnace creek", NULL);
  494. #ifdef DEMOINDEXISINVERTED
  495.         DemoBooleanRankedSearch("water | product", NULL);
  496.         DemoBooleanRankedSearch("[fluff+product]|[water+ocean ]", NULL);
  497.  
  498.         DemoBooleanRankedSearch("creek | apple", NULL);
  499.         DemoBooleanRankedSearch("[furnace+creek]|[apple+bike] ", NULL);
  500.  
  501.         DemoBooleanRankedSearch("water | product", NULL);
  502.         DemoBooleanRankedSearch("[fluff+product]|[water+ocean ]", NULL);
  503.  
  504.         DemoBooleanRankedSearch("[creek | apple]", NULL);
  505.         DemoBooleanRankedSearch("[furnace+creek]|[apple+bike] ", NULL);
  506.  
  507. #endif
  508.         DemoRelatedTerms ("microsoft");
  509.         DemoGetDocTopic();
  510.         DemoFeedback();
  511.  
  512. //        DemoDeleteAll();
  513. //        DemoDelete();
  514. //        DemoCompact();
  515.  
  516.     }
  517.     IACatch (const IAException& exception) {
  518.         printf("Caught exception: \n", exception.What());
  519.     }
  520.  
  521. #if __profile__
  522.     ProfilerDump("\pDemoAccessor.prof");
  523.     ProfilerTerm();
  524. #endif
  525.  
  526. #ifdef IADEBUG
  527.     IAReportMemoryUsage();
  528. #endif
  529.  
  530. #ifdef DEBUG_NEW
  531.     DebugNewReportLeaks();
  532. #endif
  533. }
  534.